package com.eastfair.file.service.impl;

import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.eastfair.file.props.OssProperties;
import com.eastfair.file.service.FileService;
import com.eastfair.file.vo.FileVO;
import com.eastfair.util.OrderUtil;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * 阿里云Oss文件 service.
 *
 * @author dqq
 * @date 2023 /3/17
 */
@Slf4j
@Setter
public class AliYunOssServiceImpl implements FileService {

    private OssProperties ossProperties;

    @Value("${file.subfix.verify}")
    private Boolean subfixVerify;

    private OSS ossClient;

    @Override
    public FileVO uploadFile(MultipartFile multipartFile, String bucketName) throws Exception {
        log.info("localPutFile - 阿里云文件上传, bucketName={}", bucketName);
        return uploadFile(multipartFile, bucketName, null);
    }

    @Override
    public FileVO uploadFile(MultipartFile multipartFile, String bucketName, String isMobile) throws Exception {
        String subfix = null;
        String rowFilename = null;
        String originalFilename = multipartFile.getOriginalFilename();
        String fileName = OrderUtil.getOrderNo("WJ");
        if (originalFilename != null && StrUtil.isNotBlank(originalFilename)) {
            subfix = originalFilename.lastIndexOf(".") == -1 ? originalFilename : originalFilename.substring(originalFilename.lastIndexOf("."));
            rowFilename = originalFilename.lastIndexOf(".") == -1 ? originalFilename : originalFilename.substring(0, originalFilename.lastIndexOf("."));
            fileName = fileName + subfix;
        }
        if (subfixVerify && (StrUtil.isBlank(isMobile) || !"true".equals(isMobile))) {
            // 验证扩展名
            FileService.verifySubFix(subfix);
            // 文件扩展名与文件格式是否一致
            FileService.verifyFileRowSubFix(subfix, multipartFile);
        }
        String ossPath;
        try (InputStream fis = multipartFile.getInputStream()) {
            ossPath = concatFileOSSPath(fileName);
            PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucketName(), ossPath, fis);
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setObjectAcl(CannedAccessControlList.PublicRead);
            putObjectRequest.setMetadata(metadata);
            ossClient.putObject(putObjectRequest);
        }
        //  返回文件信息
        FileVO fileVO = new FileVO();
        fileVO.setPath(ossPath);
        fileVO.setFileName(rowFilename);
        fileVO.setSize(multipartFile.getSize() / 1024);
        fileVO.setSubfix(subfix);
        return fileVO;
    }

    /**
     * 创建文件名
     *
     * @param fileName
     * @return
     */
    private String concatFileOSSPath(String fileName) {
        return "" + "/" + "/" + fileName;
    }
}